$(document).ready(function () {
    $("#error1").hide();
    $("#error2").hide();
    $("#error3").hide();
    $("#error4").hide();
});

function add() {
    var username = $("#userName").val();
    var name = $("#Name").val();
    var roleid = $("#roleid").val();
    var mail = $("#mail").val();
    var email=/^([a-zA-Z0-9_\.-]+)@(([a-zA-Z0-9_-]+)\.)+[a-zA-Z]{2,3}$/;
    if(username != null && username != '' && name != null && name != ''
        && name != null && name != '' && (email.test(mail))){
        $.ajax({
            type:"POST",
            url:"adduserOperate",
            data:{username:username, name:name, roleid:roleid, mail:mail},
            dataType:"json",
            success:function(data){
                if(data === true){
                    location.href = "userLists";
                }
                if(data === false){
                    layer.alert("添加用户失败，用户已存在");
                }
            }
        });
    }
    else {
        if(username == ''){
            $("#error1").show();
        }
        if(name == ''){
            $("#error2").show();
        }
        if(mail == ''){
            $("#error3").show();
        }
        if(!email.test(mail)){
            $("#error4").show();
            $("#mail").val("");
        }
    }
}